package bb.lanxing.mvp.presetner;


import org.json.JSONObject;

import bb.lanxing.App;
import bb.lanxing.R;
import bb.lanxing.activity.interfaces.IGetAuthCodeView;
import bb.lanxing.manager.SharedManager;
import bb.lanxing.mvp.presetner.i.BasePresenter;
import bb.lanxing.mvp.presetner.i.IGetAuthCodeModel;
import bb.lanxing.mvp.presetner.i.IGetAuthCodePresenter;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;

public class GetAuthCodePresenterImpl extends BasePresenter implements IGetAuthCodePresenter {
    private IGetAuthCodeModel model = new GetAuthCodeModelImpl();
    private IGetAuthCodeView view;

    public GetAuthCodePresenterImpl(IGetAuthCodeView iGetAuthCodeView) {
        this.view = iGetAuthCodeView;
    }

    @Override
    public void getAuthCode(String str, int i, final int i2) {
        this.view.showLoadingDialog(R.string.dialog_loading, true);
        Observable<JSONObject> authCode = this.model.getAuthCode(str, i, i2, SharedManager.getInstance().getServersAndSysTimeSub() + System.currentTimeMillis());
        if (authCode == null) {
            this.view.dismissLoadingDialog();
            this.view.toast(R.string.mine_register_toast_get_auth_fail);
            this.view.onGetAuthCodeResult(false, i2);
            return;
        }
        addSubscription(authCode.observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable th) {
                th.printStackTrace();
                App.getContext().showMessage(th.getMessage());
                view.dismissLoadingDialog();
                view.onGetAuthCodeResult(false, i2);
            }

            @Override
            public void onNext(JSONObject jsonObject) {
                view.dismissLoadingDialog();
                view.toast(R.string.mine_register_toast_get_auth_successful);
                view.onGetAuthCodeResult(jsonObject);
            }
        }));
    }
}
